package com.flurry.sdk;

import android.os.Build;
import com.flurry.sdk.iv;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.zip.CRC32;

public class iu {
    public static final String b = iu.class.getName();
    private static iu c = null;
    public String a;
    /* access modifiers changed from: private */
    public kf<List<iv>> d;
    /* access modifiers changed from: private */
    public List<iv> e;
    private boolean f;

    private iu() {
    }

    public static synchronized iu a() {
        iu iuVar;
        synchronized (iu.class) {
            if (c == null) {
                iu iuVar2 = new iu();
                c = iuVar2;
                iuVar2.d = new kf<>(jy.a().a.getFileStreamPath(".yflurrypulselogging." + Long.toString(ly.i(jy.a().d), 16)), ".yflurrypulselogging.", 1, new lj<List<iv>>() {
                    public final lg<List<iv>> a(int i) {
                        return new lf(new iv.a());
                    }
                });
                iuVar2.f = ((Boolean) lp.a().a("UseHttps")).booleanValue();
                km.a(4, b, "initSettings, UseHttps = " + iuVar2.f);
                iuVar2.e = iuVar2.d.a();
                if (iuVar2.e == null) {
                    iuVar2.e = new ArrayList();
                }
            }
            iuVar = c;
        }
        return iuVar;
    }

    public final synchronized void a(it itVar) {
        try {
            this.e.add(new iv(itVar.d()));
            km.a(4, b, "Saving persistent Pulse logging data.");
            this.d.a(this.e);
        } catch (IOException e2) {
            km.a(6, b, "Error when generating pulse log report in addReport part");
        }
        return;
    }

    private byte[] d() throws IOException {
        DataOutputStream dataOutputStream;
        byte[] bArr;
        DataOutputStream dataOutputStream2 = null;
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            dataOutputStream = new DataOutputStream(byteArrayOutputStream);
            try {
                if (this.e == null || this.e.isEmpty()) {
                    bArr = byteArrayOutputStream.toByteArray();
                    ly.a((Closeable) dataOutputStream);
                } else {
                    dataOutputStream.writeShort(1);
                    dataOutputStream.writeShort(1);
                    dataOutputStream.writeLong(System.currentTimeMillis());
                    dataOutputStream.writeUTF(jy.a().d);
                    dataOutputStream.writeUTF(ju.a().g());
                    dataOutputStream.writeShort(jz.a());
                    dataOutputStream.writeShort(3);
                    ju.a();
                    dataOutputStream.writeUTF(ju.c());
                    dataOutputStream.writeBoolean(jl.a().c());
                    ArrayList<ia> arrayList = new ArrayList<>();
                    for (Map.Entry next : Collections.unmodifiableMap(jl.a().a).entrySet()) {
                        ia iaVar = new ia();
                        iaVar.a = ((jt) next.getKey()).c;
                        if (((jt) next.getKey()).d) {
                            iaVar.b = new String((byte[]) next.getValue());
                        } else {
                            iaVar.b = ly.b((byte[]) next.getValue());
                        }
                        arrayList.add(iaVar);
                    }
                    dataOutputStream.writeShort(arrayList.size());
                    for (ia iaVar2 : arrayList) {
                        dataOutputStream.writeShort(iaVar2.a);
                        byte[] bytes = iaVar2.b.getBytes();
                        dataOutputStream.writeShort(bytes.length);
                        dataOutputStream.write(bytes);
                    }
                    dataOutputStream.writeShort(6);
                    dataOutputStream.writeShort(in.MODEL.h);
                    dataOutputStream.writeUTF(Build.MODEL);
                    dataOutputStream.writeShort(in.BRAND.h);
                    dataOutputStream.writeUTF(Build.BOARD);
                    dataOutputStream.writeShort(in.ID.h);
                    dataOutputStream.writeUTF(Build.ID);
                    dataOutputStream.writeShort(in.DEVICE.h);
                    dataOutputStream.writeUTF(Build.DEVICE);
                    dataOutputStream.writeShort(in.PRODUCT.h);
                    dataOutputStream.writeUTF(Build.PRODUCT);
                    dataOutputStream.writeShort(in.VERSION_RELEASE.h);
                    dataOutputStream.writeUTF(Build.VERSION.RELEASE);
                    dataOutputStream.writeShort(this.e.size());
                    for (iv ivVar : this.e) {
                        dataOutputStream.write(ivVar.a);
                    }
                    byte[] byteArray = byteArrayOutputStream.toByteArray();
                    CRC32 crc32 = new CRC32();
                    crc32.update(byteArray);
                    dataOutputStream.writeInt((int) crc32.getValue());
                    bArr = byteArrayOutputStream.toByteArray();
                    ly.a((Closeable) dataOutputStream);
                }
                return bArr;
            } catch (IOException e2) {
                e = e2;
                dataOutputStream2 = dataOutputStream;
                try {
                    km.a(6, b, "Error when generating report", e);
                    throw e;
                } catch (Throwable th) {
                    th = th;
                    dataOutputStream = dataOutputStream2;
                    ly.a((Closeable) dataOutputStream);
                    throw th;
                }
            } catch (Throwable th2) {
                th = th2;
                ly.a((Closeable) dataOutputStream);
                throw th;
            }
        } catch (IOException e3) {
            e = e3;
            km.a(6, b, "Error when generating report", e);
            throw e;
        } catch (Throwable th3) {
            th = th3;
            dataOutputStream = null;
            ly.a((Closeable) dataOutputStream);
            throw th;
        }
    }

    /* JADX WARNING: type inference failed for: r6v0, types: [RequestObjectType, byte[]] */
    /* JADX WARNING: Unknown variable types count: 1 */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private synchronized void a(byte[] r6) {
        /*
            r5 = this;
            monitor-enter(r5)
            com.flurry.sdk.jr r0 = com.flurry.sdk.jr.a()     // Catch:{ all -> 0x0021 }
            boolean r0 = r0.b     // Catch:{ all -> 0x0021 }
            if (r0 != 0) goto L_0x0013
            r0 = 5
            java.lang.String r1 = b     // Catch:{ all -> 0x0021 }
            java.lang.String r2 = "Reports were not sent! No Internet connection!"
            com.flurry.sdk.km.a((int) r0, (java.lang.String) r1, (java.lang.String) r2)     // Catch:{ all -> 0x0021 }
        L_0x0011:
            monitor-exit(r5)
            return
        L_0x0013:
            if (r6 == 0) goto L_0x0018
            int r0 = r6.length     // Catch:{ all -> 0x0021 }
            if (r0 != 0) goto L_0x0024
        L_0x0018:
            r0 = 3
            java.lang.String r1 = b     // Catch:{ all -> 0x0021 }
            java.lang.String r2 = "No report need be sent"
            com.flurry.sdk.km.a((int) r0, (java.lang.String) r1, (java.lang.String) r2)     // Catch:{ all -> 0x0021 }
            goto L_0x0011
        L_0x0021:
            r0 = move-exception
            monitor-exit(r5)
            throw r0
        L_0x0024:
            java.lang.String r0 = r5.a     // Catch:{ all -> 0x0021 }
            if (r0 == 0) goto L_0x007f
            java.lang.String r0 = r5.a     // Catch:{ all -> 0x0021 }
        L_0x002a:
            r1 = 4
            java.lang.String r2 = b     // Catch:{ all -> 0x0021 }
            java.lang.StringBuilder r3 = new java.lang.StringBuilder     // Catch:{ all -> 0x0021 }
            java.lang.String r4 = "PulseLoggingManager: start upload data "
            r3.<init>(r4)     // Catch:{ all -> 0x0021 }
            java.lang.String r4 = java.util.Arrays.toString(r6)     // Catch:{ all -> 0x0021 }
            java.lang.StringBuilder r3 = r3.append(r4)     // Catch:{ all -> 0x0021 }
            java.lang.String r4 = " to "
            java.lang.StringBuilder r3 = r3.append(r4)     // Catch:{ all -> 0x0021 }
            java.lang.StringBuilder r3 = r3.append(r0)     // Catch:{ all -> 0x0021 }
            java.lang.String r3 = r3.toString()     // Catch:{ all -> 0x0021 }
            com.flurry.sdk.km.a((int) r1, (java.lang.String) r2, (java.lang.String) r3)     // Catch:{ all -> 0x0021 }
            com.flurry.sdk.ks r1 = new com.flurry.sdk.ks     // Catch:{ all -> 0x0021 }
            r1.<init>()     // Catch:{ all -> 0x0021 }
            r1.f = r0     // Catch:{ all -> 0x0021 }
            r0 = 100000(0x186a0, float:1.4013E-40)
            r1.w = r0     // Catch:{ all -> 0x0021 }
            com.flurry.sdk.ku$a r0 = com.flurry.sdk.ku.a.kPost     // Catch:{ all -> 0x0021 }
            r1.g = r0     // Catch:{ all -> 0x0021 }
            r0 = 1
            r1.j = r0     // Catch:{ all -> 0x0021 }
            java.lang.String r0 = "Content-Type"
            java.lang.String r2 = "application/octet-stream"
            r1.a(r0, r2)     // Catch:{ all -> 0x0021 }
            com.flurry.sdk.lc r0 = new com.flurry.sdk.lc     // Catch:{ all -> 0x0021 }
            r0.<init>()     // Catch:{ all -> 0x0021 }
            r1.c = r0     // Catch:{ all -> 0x0021 }
            r1.b = r6     // Catch:{ all -> 0x0021 }
            com.flurry.sdk.iu$2 r0 = new com.flurry.sdk.iu$2     // Catch:{ all -> 0x0021 }
            r0.<init>()     // Catch:{ all -> 0x0021 }
            r1.a = r0     // Catch:{ all -> 0x0021 }
            com.flurry.sdk.jw r0 = com.flurry.sdk.jw.a()     // Catch:{ all -> 0x0021 }
            r0.a((java.lang.Object) r5, r1)     // Catch:{ all -> 0x0021 }
            goto L_0x0011
        L_0x007f:
            java.lang.String r0 = "https://data.flurry.com/pcr.do"
            goto L_0x002a
        */
        throw new UnsupportedOperationException("Method not decompiled: com.flurry.sdk.iu.a(byte[]):void");
    }

    public final synchronized void b() {
        try {
            a(d());
        } catch (IOException e2) {
            km.a(6, b, "Report not send due to exception in generate data");
        }
        return;
    }
}
